import java.util.HashMap;
import java.util.Map;

public class Num3 {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> sMap = new HashMap<>();
        int ans = 0;
        int n = s.length();

        for (int i = 0, j = 0; i < n && j < n ; j++){
            Character c = s.charAt(j);
            if (sMap.containsKey(c)){
                i = Math.max(sMap.get(c), i);
            }
            ans = Math.max(ans, j - i + 1);
            sMap.put(c, j + 1);

            if (ans >= n-i) return ans;
        }
        return ans;
    }
}
